<?php
/**
 * Created by PhpStorm.
 * User: Auser
 * Date: 2016/12/13
 * Time: 14:06
 */

namespace App\Services;
use TestingDetailService;
use ResultTypeService;
use XjbFactorService;
use TestingXjbAssistHelper;
class TestingXjbHelper
{
    public function conclusion($tid,$scorestr,$answerArr,$score){
        $scoreArr = explode(',',$scorestr);
        $sex = TestingXjbAssistHelper::getSex($answerArr);
        if($sex['sex'] == 1){
            if($score <= 0.2){
                $score = 0.1637*exp($score);
            }
            $pro = $scoreArr[0]*$score;
            $pro = number_format($pro,6);
        }else{
            if($tid == 4){
                $pro = 0;
            }else{
                if($score <= 0.2){
                    $score = 0.1637*exp($score);
                }
                $pro = $scoreArr[1]*$score;
                $pro = number_format($pro,6);
            }
        }
        return $pro;
    }


    /**返回因素
     * @param $answerArr 答题提交记录
     * @param $tid       测评题目id
     * @param $typeFourExpress  是否有第四种类型
     * @param $score      测评概率
     * @param $final_answers   组合后答案字符串
     * @return array
     */
    public function getFactor($answerArr,$tid,$typeFourExpress,$score,$final_answers){
        $sexResult = TestingXjbAssistHelper::getSex($answerArr);
        if($sexResult['sex'] == 0 && $tid==4){
            $data = array(
                'better_factor'=>null,
                'increase_factor'=>null,
                'lower_factor'=>null
            );
        }else{
            $data = array(
                'better_factor'=>$this->betterFactor($tid,$answerArr,$typeFourExpress,$score,$final_answers),
                'increase_factor'=>$this->increaseFactor($tid,$answerArr,$final_answers),
                'lower_factor'=>$this -> lowerFactor($tid,$answerArr,$final_answers)
            );
        }
        return$data;
    }


    public function getSingleFactor($answerArr,$tid,$final_answers){
        $sexResult = TestingXjbAssistHelper::getSex($answerArr);
        if($sexResult['sex'] == 0 && $tid==4){
            $data = array(
                'increase_factor'=>null,
                'lower_factor'=>null
            );
        }else{
            $data = array(
                'increase_factor'=>$this->increaseFactor($tid,$answerArr,$final_answers),
                'lower_factor'=>$this -> lowerFactor($tid,$answerArr,$final_answers)
            );
        }
        return$data;
    }

    /**返回可改善因素
     * @param $tid
     * @param $answerArr
     * @param $typeFourExpress
     * @param $score
     * @param $final_answers
     * @return array
     */
    public function betterFactor($tid,$answerArr,$typeFourExpress,$score,$final_answers){
        $betterFactor = [];
        foreach($answerArr as $k => $v){
            $factor = XjbFactorService::factor((int)$tid,(int)$v['index'],(float)$v['weight'],'1');
            if($factor){
                $data['tag'] =$factor['tag'];
                $data['tips']=$factor['tips'];
                $data['answerAgain']=$v['index'].":".$factor['relativeresult'].":"."0";
               /* $weight = $factor['relativeresult'];
                $answerArr[$k]['weight']=$weight;
                $answerStr = TestingXjbAssistHelper::arrToStr($answerArr);
                $relativeScore = $this -> relativeScore($tid,$answerStr,$typeFourExpress);
                $betterScore = $relativeScore-$score;
                $data['lower_score']=$betterScore;*/
                array_push($betterFactor,$data);
                //$answerArr[$k]['weight']=$factor['result'];
            }
        }
        //添加额外可改善风险因素
        $extraBetterFactor = $this->extraBetterFactor($tid,$final_answers,$answerArr,$typeFourExpress,$score);
        if(!empty($extraBetterFactor)){
            foreach($extraBetterFactor as $k => $v){
                if(!empty($v)){
                    array_push($betterFactor,$v);
                }
            }
        }
      if(count($betterFactor)>0){
            return $betterFactor;
        }else{
            return ['tag'=>null,'tips'=>null];
        }

   }

    /**额外改善因素
     * @param $tid
     * @param $final_answers
     * @param $answerArr
     * @param $typeFourExpress
     * @param $score
     * @return array
     */
    public function extraBetterFactor($tid,$final_answers,$answerArr,$typeFourExpress,$score){
        $extraBetter = [];
        switch($tid){
            case 6:
                $extraBetter[0] = $this->weightFactor($tid,$final_answers,'better',$answerArr,$typeFourExpress,$score);
                break;
            case 7:
                $extraBetter[0] = $this->weightFactor($tid,$final_answers,'better',$answerArr,$typeFourExpress,$score);
                $extraBetter[1] = $this->smokingFactor($tid,$final_answers,'better',$answerArr,$typeFourExpress,$score);
                break;
            case 9:
                $extraBetter[1] = $this->smokingFactor($tid,$final_answers,'better',$answerArr,$typeFourExpress,$score);
                break;
            case 10:
                $extraBetter[1] = $this->smokingFactor($tid,$final_answers,'better',$answerArr,$typeFourExpress,$score);
                break;
            case 11:
                $extraBetter[1] = $this->smokingFactor($tid,$final_answers,'better',$answerArr,$typeFourExpress,$score);
                break;

        }
        return $extraBetter;
    }


    /**返回增加因素
     * @param $tid
     * @param $answerArr
     * @param $final_answers
     * @return array
     */
    public function increaseFactor($tid,$answerArr,$final_answers){
        $increaseFactor = [];
        foreach($answerArr as $k => $v){
            $factor = XjbFactorService::factor((int)$tid,(int)$v['index'],(float)$v['weight'],'2');
            if($factor){
                $data['tag'] = $factor['tag'];
                $data['tips'] = $factor['tips'];
                array_push($increaseFactor,$data);
            }
        }
        //添加额外增加风险因素
        $extraIncreaseFactor = $this ->extraIncreaseFactor($tid,$final_answers);
        if(!empty($extraIncreaseFactor)){
            foreach($extraIncreaseFactor as $k => $v){
                if(!empty($v)){
                    array_push($increaseFactor,$v);
                }
            }
        }
        if(count($increaseFactor)>0){
           return $increaseFactor;
        }else{
            return ['tag'=>null,'tips'=>null];
        }
    }

    /**额外增加因素
     * @param $tid
     * @param $final_answers
     * @return array
     */
    public function extraIncreaseFactor($tid,$final_answers){
        $extraIncrease = [];
        switch($tid){
            case 6:
                $extraIncrease[0] = $this->weightFactor($tid,$final_answers,'increase');
                break;
            case 7:
                $extraIncrease[0] =$this->weightFactor($tid,$final_answers,'increase');
                $extraIncrease[1] =$this->smokingFactor($tid,$final_answers,'increase');
                break;
            case 9:
                $extraIncrease[1] =$this->smokingFactor($tid,$final_answers,'increase');
                break;
            case 10:
                $extraIncrease[1] =$this->smokingFactor($tid,$final_answers,'increase');
                break;
            case 11:
                $extraIncrease[1] =$this->smokingFactor($tid,$final_answers,'increase');
                break;
        }
        return $extraIncrease;
    }

    /**返回降低因素
     * @param $tid
     * @param $answerArr
     * @return array
     */
    public function lowerFactor($tid,$answerArr,$final_answers){
        $lowerFactor = [];
        foreach($answerArr as $k => $v){
            $factor = XjbFactorService::factor((int)$tid,(int)$v['index'],(float)$v['weight'],'3');
            if($factor){
                $data['tag'] = $factor['tag'];
                $data['tips'] = $factor['tips'];
                array_push($lowerFactor,$data);
            }
        }
        //添加额外降低风险因素
        $extraLowerFactor = $this ->extraLowerFactor($tid,$final_answers);
        if(!empty($extraLowerFactor)){
            foreach($extraLowerFactor as $k => $v){
                if(!empty($v)){
                    array_push($lowerFactor,$v);
                }
            }
        }
        if(count($lowerFactor)>0){
            return $lowerFactor;
        }else{
            return ['tag'=>null,'tips'=>null];
        }
    }

    /**额外降低因素
     * @param $tid
     * @param $final_answers
     * @return array
     */
    public function extraLowerFactor($tid,$final_answers){
        $extraLower = [];
        switch($tid){
            case 6:
                $extraLower[0] = $this->weightFactor($tid,$final_answers,'lower');
                break;
            case 7:
                $extraLower[0] = $this->weightFactor($tid,$final_answers,'lower');
                $extraLower[1] = $this->smokingFactor($tid,$final_answers,'lower');
                break;
            case 9:
                $extraLower[1] = $this->smokingFactor($tid,$final_answers,'lower');
                break;
            case 10:
                $extraLower[1] = $this->smokingFactor($tid,$final_answers,'lower');
                break;
            case 11:
                $extraLower[1] = $this->smokingFactor($tid,$final_answers,'lower');
                break;
        }
        return $extraLower;
    }


    /**BMI额外因素
     * @param $tid
     * @param $final_answers
     * @param $answerArr
     * @param $type
     * @param null $typeFourExpress
     * @param null $score
     * @return array
     */
    public function weightFactor($tid,$final_answers,$type=null,$answerArr=null,$typeFourExpress=null,$score=null){
        $finalAnswers_arr= TestingXjbAssistHelper::strToArr($final_answers);
        $extra = [];
        $data = TestingXjbAssistHelper::weightparameter($finalAnswers_arr,$tid);
        $BMI =(int)$data['bmi'];
        $height = $data['height'];
        $weightindex = $data['weigntindex'];
            if($BMI >= 27 && $type ==  'better'){
                $extra['tag'] = "控制您的体重在理想体重范围（".($height-105)*0.9."kg~".($height-105)*1.1."kg）。";
                $extra['tips'] = "<p>您的体重已超标，建议您适当减轻体重，更有利于您的健康。</p></br><p>有研究表明，肥胖可以增加结肠癌33%的发病风险。这可能与脂肪组织会影响体内激素水平有关。</p></br><p>1)	你的适宜体重范围是".($height-105)*0.9."kg~".($height-105)*1.1."kg</p></br><p>2)	请使用科学的方式减重。一些不当的减肥方式会损害身体健康，例：节食，减肥药等等。</p>";
                $normalWeight = ($height-105)*1;
                $extra['answerAgain']=$weightindex.":"."0".":".$normalWeight;

               /* foreach($answerArr as $k => $v){
                    if($v['index'] == $weightindex){
                        $answerArr[$k]['answer'] = $normalWeight;
                    }
                }
                $answerStr = TestingXjbAssistHelper::arrToStr($answerArr);
                $relativeScore = $this->relativeScore($tid,$answerStr,$typeFourExpress);
                $betterScore = $relativeScore-$score;
                $extra['lower_score']=$betterScore;*/
            }elseif($BMI >= 27 && $type == "increase"){
                $extra['tag'] = "您的BMI为 ".number_format($BMI,2)."，为超重/肥胖状态。";
                $extra['tips'] = "<p>您的体重已超标，建议您适当减轻体重，更有利于您的健康。</p></br><p>有研究表明，肥胖可以增加结肠癌33%的发病风险。这可能与脂肪组织会影响体内激素水平有关。</p></br><p>1)	你的适宜体重范围是".($height-105)*0.9."kg~".($height-105)*1.1."kg</p></br><p>2)	请使用科学的方式减重。一些不当的减肥方式会损害身体健康，例：节食，减肥药等等。</p>";
            }
            elseif($BMI < 27 && $type == 'lower'){
                $extra['tag'] = "您的BMI为 ".number_format($BMI,2)."，为标准状态。";
                $extra['tips']="<p>您的体重非常合适，请继续保持，有利于您的健康。</p></br><p>有研究表明，肥胖可以增加结肠癌33%的发病风险。这可能与脂肪组织会影响体内激素水平有关。</p></br><p>1)	你的适宜体重范围是".($height-105)*0.9."kg~".($height-105)*1.1."kg</p></br><p>2)	请使用科学的方式减重。一些不当的减肥方式会损害身体健康，例：节食，减肥药等等。</p>";
            }
        return $extra;
    }

    /**吸烟额外因素
     * @param $tid
     * @param $final_answers
     * @param $answerArr
     * @param $type
     * @param null $typeFourExpress
     * @param null $score
     * @return array
     */
    public function smokingFactor($tid,$final_answers,$type=null,$answerArr=null,$typeFourExpress=null,$score=null){
        $finalAnswers_arr= TestingXjbAssistHelper::strToArr($final_answers);
        $extra = [];
        $data = TestingXjbAssistHelper::smokingparameter($finalAnswers_arr,$tid);
        $smoking = (int)$data['smoking'];
        $smokingindex=$data['smokingindex'];
        $special_type=array(10,11);//判断条件不同
        if(in_array($tid,$special_type)){
               if($smoking > 0 && $type == "better"){
                   $extra['tag'] = "戒烟或减少吸烟量。";
                   if($tid == 10){
                       $extra['tips'] = "<p>吸烟是膀胱癌的重要危险因素。纸烟在燃烧过程中会发生4000余种新的化学物质其中绝大部分对人体有害，包括尼古丁、一氧化碳和烟焦油。每天吸烟量越大，吸烟持续时间越长，吸烟总量越大患膀胱癌的风险性就越大。香烟中的一些致癌物质经过各种途径吸收转化后从尿中排出，可直接引起膀胱上皮细胞转化，增加膀胱癌的患病风险。戒烟可以减缓肿瘤的发展过程。</p></br><p>1)	戒烟可以降低膀胱癌的风险。请您戒烟或减少吸烟量。</p>";
                   }elseif($tid == 11){
                       $extra['tips'] = "<p>吸烟已被公认为肺癌的首位危险因素，90%的肺癌患者的发病与吸烟有关。纸烟在燃烧过程中会发生4000余种新的化学物质其中绝大部分对人体有害，包括尼古丁、一氧化碳和烟焦油。</p></br><p>1)	每天吸烟量越大，吸烟持续时间越长，吸烟总量越大患肺癌的风险性就越大。</p>";
                   }
                   $extra['answerAgain']= $smokingindex.":"."0".":"."0";
                  /* foreach($answerArr as $k => $v){
                       if($v['index'] == $smokingindex){
                           $answerArr[$k]['answer'] = 0;
                       }
                   }
                   $answerStr = TestingXjbAssistHelper::arrToStr($answerArr);
                   $relativeScore = $this->relativeScore($tid,$answerStr,$typeFourExpress);
                   $betterScore = $relativeScore-$score;
                   $extra['lower_score']=$betterScore;*/
               }elseif($smoking > 0 && $type == "increase"){
                   $extra['tag'] = "您平均每天吸烟数量比较大!";
                   if($tid == 10){
                       $extra['tips'] = "<p>吸烟是膀胱癌的重要危险因素。纸烟在燃烧过程中会发生4000余种新的化学物质其中绝大部分对人体有害，包括尼古丁、一氧化碳和烟焦油。每天吸烟量越大，吸烟持续时间越长，吸烟总量越大患膀胱癌的风险性就越大。香烟中的一些致癌物质经过各种途径吸收转化后从尿中排出，可直接引起膀胱上皮细胞转化，增加膀胱癌的患病风险。戒烟可以减缓肿瘤的发展过程。</p></br><p>1)	戒烟可以降低膀胱癌的风险。请您戒烟或减少吸烟量。</p>";
                   }elseif($tid == 11){
                       $extra['tips'] = "<p>吸烟已被公认为肺癌的首位危险因素，90%的肺癌患者的发病与吸烟有关。纸烟在燃烧过程中会发生4000余种新的化学物质其中绝大部分对人体有害，包括尼古丁、一氧化碳和烟焦油。</p></br><p>1)	每天吸烟量越大，吸烟持续时间越长，吸烟总量越大患肺癌的风险性就越大。</p>";
                   }
               }elseif($smoking <= 0 &&  $type == 'lower'){
                   $extra['tag'] = "您不吸烟或已经戒烟。";
                   if($tid == 10){
                       $extra['tips'] = "<p>吸烟是膀胱癌的重要危险因素。纸烟在燃烧过程中会发生4000余种新的化学物质其中绝大部分对人体有害，包括尼古丁、一氧化碳和烟焦油。每天吸烟量越大，吸烟持续时间越长，吸烟总量越大患膀胱癌的风险性就越大。香烟中的一些致癌物质经过各种途径吸收转化后从尿中排出，可直接引起膀胱上皮细胞转化，增加膀胱癌的患病风险。戒烟可以减缓肿瘤的发展过程。</p></br><p>1)	戒烟可以降低膀胱癌的风险。请您戒烟或减少吸烟量。</p>";
                   }elseif($tid == 11){
                       $extra['tips'] = "<p>吸烟已被公认为肺癌的首位危险因素，90%的肺癌患者的发病与吸烟有关。纸烟在燃烧过程中会发生4000余种新的化学物质其中绝大部分对人体有害，包括尼古丁、一氧化碳和烟焦油。</p></br><p>1)	每天吸烟量越大，吸烟持续时间越长，吸烟总量越大患肺癌的风险性就越大。</p>";
                   }
               }
        }else{
            if($smoking >= 15 && $type == "better"){
                $extra['tag'] = "戒烟或减少吸烟量。";
                if($tid == 7){
                    $extra['tips'] = "<p>吸烟是肾癌唯一公认的环境危险因素。纸烟在燃烧过程中会发生4000余种新的化学物质其中绝大部分对人体有害，包括尼古丁、一氧化碳和烟焦油。有害物质存在于尿液之中，经肾进入膀胱，增加肾癌风险。</p></br><p>1)	戒烟可以降低肾癌的风险。请您戒烟或减少吸烟量。</p>";
                }elseif($tid == 9){
                    $extra['tips'] = "<p>吸烟是胰腺癌的重要危险因素。纸烟在燃烧过程中会发生4000余种新的化学物质其中绝大部分对人体有害，包括尼古丁、一氧化碳和烟焦油。这些致癌物质经肺或上消化道进入人体内后血液进入胰腺，或经十二指肠反流入胰腺。每天吸烟量越大，吸烟持续时间越长，吸烟总量越大患胰腺癌的风险性就越大。</p></br><p>1)  戒烟可降低胰腺癌的发病风险，请您戒烟或减少吸烟量。</p>";
                }
                $extra['answerAgain']= $smokingindex.":"."0".":"."0";

               /* foreach($answerArr as $k => $v){
                    if($v['index'] == $smokingindex){
                        $answerArr[$k]['answer'] = 0;
                    }
                }
                $answerStr = TestingXjbAssistHelper::arrToStr($answerArr);
                $relativeScore = $this->relativeScore($tid,$answerStr,$typeFourExpress);
                $betterScore = $relativeScore-$score;
                $extra['lower_score']=$betterScore;*/
            }elseif($smoking >= 15 && $type == "increase"){
                $extra['tag'] = "您平均每天吸烟数量比较大!";
                if($tid == 7){
                    $extra['tips'] = "<p>吸烟是肾癌唯一公认的环境危险因素。纸烟在燃烧过程中会发生4000余种新的化学物质其中绝大部分对人体有害，包括尼古丁、一氧化碳和烟焦油。有害物质存在于尿液之中，经肾进入膀胱，增加肾癌风险。</p></br><p>1)	戒烟可以降低肾癌的风险。请您戒烟或减少吸烟量。</p>";
                }elseif($tid == 9){
                    $extra['tips'] = "<p>吸烟是胰腺癌的重要危险因素。纸烟在燃烧过程中会发生4000余种新的化学物质其中绝大部分对人体有害，包括尼古丁、一氧化碳和烟焦油。这些致癌物质经肺或上消化道进入人体内后血液进入胰腺，或经十二指肠反流入胰腺。每天吸烟量越大，吸烟持续时间越长，吸烟总量越大患胰腺癌的风险性就越大。</p></br><p>1)  戒烟可降低胰腺癌的发病风险，请您戒烟或减少吸烟量。</p>";
                }
            }elseif($smoking < 15 &&  $type == 'lower'){
                $extra['tag'] = "您不吸烟，吸烟量少或已经戒烟。";
                if($tid == 7){
                    $extra['tips'] = "<p>吸烟是肾癌唯一公认的环境危险因素。纸烟在燃烧过程中会发生4000余种新的化学物质其中绝大部分对人体有害，包括尼古丁、一氧化碳和烟焦油。有害物质存在于尿液之中，经肾进入膀胱，增加肾癌风险。</p></br><p>1)	戒烟可以降低肾癌的风险。请您戒烟或减少吸烟量。</p>";
                }elseif($tid == 9){
                    $extra['tips'] = "<p>吸烟是胰腺癌的重要危险因素。纸烟在燃烧过程中会发生4000余种新的化学物质其中绝大部分对人体有害，包括尼古丁、一氧化碳和烟焦油。这些致癌物质经肺或上消化道进入人体内后血液进入胰腺，或经十二指肠反流入胰腺。每天吸烟量越大，吸烟持续时间越长，吸烟总量越大患胰腺癌的风险性就越大。</p></br><p>1)  戒烟可降低胰腺癌的发病风险，请您戒烟或减少吸烟量。</p>";
                }
            }
        }
            return $extra;
    }

    /**重新计算接口
     * @param $answers 提交字符串
     * @param $tid
     * @param $typeFourExpress
     * @param $answerAgain 改善提交字符串
     * @return mixed
     */
    public function retesting($answers,$tid,$typeFourExpress,$answerAgain){
            $answersArr = $this->strToArr($answers);
            $answerAgainArr = $this->strToArr($answerAgain);
        foreach($answersArr as $k => $v){
            foreach($answerAgainArr as $key => $value){
                if($value['index'] == $v['index']){
                    $answersArr[$k]['index']=$value['index'];
                    $answersArr[$k]['weight']=$value['weight'];
                    $answersArr[$k]['answer']=$value['answer'];
                }
            }
        }
        $answerAgainStr = TestingXjbAssistHelper::arrToStr($answersArr);
        $relativeScore = $this->relativeScore($tid,$answerAgainStr,$typeFourExpress);
        return $relativeScore;
    }

    public function avgScore($tid,$answers){
        $answerArr=  $this->strToArr($answers);
        $sex = TestingXjbAssistHelper::getSex($answerArr);
        if($sex['sex'] == 1){//男性
            switch ($tid) {
                case 4://前列腺癌
                    //$avg = 0.026.mt_rand(1,9);
                    $avg = 0.0262;
                    break;
                case 5://胃癌
                    //$avg = 0.071.mt_rand(1,9);
                    $avg = 0.0719;
                    break;
                case 6: //结肠癌
                    //$avg = 0.151.mt_rand(1,9);
                    $avg = 0.1519;
                    break;
                case 7://肾癌
                    //$avg = 0.031.mt_rand(1,9);
                    $avg = 0.0319;
                    break;
                case 8://黑色素瘤
                    //$avg = 0.001.mt_rand(1,9);
                    $avg = 0.001;
                    break;
                case 9://胰腺癌
                    //$avg = 0.009.mt_rand(1,9);
                    $avg = 0.0091;
                    break;
                case 10://膀胱癌
                    //$avg = 0.019.mt_rand(1,9);
                    $avg = 0.0194;
                    break;
                case 11://肺癌
                    //$avg = 0.067.mt_rand(1,9);
                    $avg = 0.0672;
                    break;
                default:
                    $avg = 0.0043;
                    break;
            }
        }else{
            switch ($tid) {
                case 4://前列腺癌
                    $avg = 1;
                    break;
                case 5://胃癌
                    //$avg = 0.033.mt_rand(1,9);
                    $avg = 0.0332;
                    break;
                case 6: //结肠癌
                    //$avg = 0.118.mt_rand(1,9);
                    $avg = 0.1187;
                    break;
                case 7://肾癌
                    //$avg = 0.018.mt_rand(1,9);
                    $avg = 0.0185;
                    break;
                case 8://黑色素瘤
                    //$avg = 0.001.mt_rand(1,9);
                    $avg = 0.0011;
                    break;
                case 9://胰腺癌
                    //$avg = 0.007.mt_rand(1,9);
                    $avg = 0.0076;
                    break;
                case 10://膀胱癌
                    //$avg = 0.005.mt_rand(1,9);
                    $avg = 0.0055;
                    break;
                case 11://肺癌
                    //$avg = 0.036.mt_rand(1,9);
                    $avg = 0.0362;
                    break;
                default:
                    $avg =0.0043;
                    break;
            }
        }
        return (float)$avg;
    }

        /**根据答案字符串获取score
     * @param $tid
     * @param $answerStr
     * @param $typeFourExpress
     * @return mixed
     */
   public function relativeScore($tid,$answerStr,$typeFourExpress){
        $reslut = TestingXjbAssistHelper::calculate($tid,$answerStr,$typeFourExpress);
        $Probability=$this -> testingProbability($tid,$reslut['totalScore'],$answerStr);
        $relativeScore=$Probability['score']*100;
        return $relativeScore;
    }


    public function testingProbability($tid,$score,$answerstr){
        $detail = TestingDetailService::getById($tid);
        if($detail) {
            $result_type_id_arr = explode(',', $detail['result_type']);
            $result = ResultTypeService::result($tid,$result_type_id_arr,$score);
            $answers =TestingXjbAssistHelper::strToArr($answerstr);
            $result['score']=$this->conclusion($tid,$result['factor_str'],$answers,$score);
            unset($result['factor_str']);
            return $result;
        }else{
            return null;
        }
    }

    /***将答案字符串转换为数组
     * @param $answerStr
     * @return array
     */
    public function strToArr($answerStr){
        $answers = [];
        $answerArr = explode('##',$answerStr);
        if(count($answerArr)>0){
            foreach ($answerArr as $answer){
                $singleArr = explode(':',$answer);
                if(count($singleArr)>0){
                    $obj['index'] = $singleArr[0];
                    $obj['weight'] = $singleArr[1];
                    $obj['answer'] = $singleArr[2];
                    array_push($answers,$obj);
                }
            }
        }
        return $answers;
    }

}